package com.jf.anmocker.plugin.mockcore.asm

import org.objectweb.asm.Opcodes
import org.objectweb.asm.Type

class AnMockerFilter {

    companion object {

        fun isInterface(access: Int) : Boolean {
            return (access and Opcodes.ACC_INTERFACE) != 0
        }

        fun isEnum(access: Int) : Boolean{
            return access == Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_SUPER + Opcodes.ACC_ENUM
        }

        fun isSameParams(methodDescriptor : String, params: ArrayList<String>?): Boolean {
            val types = Type.getArgumentTypes(methodDescriptor)
            val typeSize = types?.size ?: 0
            val paramSize = params?.size ?: 0
            if(typeSize == 0 && paramSize == 0){
                return true
            }else if(typeSize == paramSize){
                for((index, value) in types.withIndex()){
                    if(!checkType(value, params?.get(index))){
                        return false
                    }
                }
                return true
            }
            return false
        }

        private fun checkType(type : Type, typeStr : String?) : Boolean{
            if(typeStr == null){
                return false
            }
            if(type.className.equals(typeStr)){
                return true
            }else if(type.className == "java.lang.String" && typeStr == "String"){
                return true
            }
            return false
        }
    }

}